//
//  NetWork.m
//  Tospur-text
//
//  Created by Tospur_hewei on 15/10/9.
//  Copyright (c) 2015年 Tospur_hewei. All rights reserved.
//

#import "NetWork.h"
#import <AFNetworking.h>
#import "Encryption.h"
#import "BuildDetailModel.h"
@implementation NetWork
static NetWork *netWork;
+ (NetWork *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        netWork = [[NetWork alloc]init];
        netWork.sessionid = @"";
    });
    return netWork;
}

// 启动页请求图片
- (NSMutableArray *)postImageUrl
{
    NSMutableArray *imageUrlArray = [NSMutableArray array];
    AFHTTPRequestOperationManager *tx = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    tx.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    tx.requestSerializer = [AFJSONRequestSerializer serializer];
    // [tx.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    NSString *urlStr = @"http://172.20.5.142:7001/api/GetOnloadImg";
    NSString *signature = [[Encryption shareInstance]sha1];
    NSLog(@"+++++ %@",signature);
    NSDictionary *dict = @{
                           @"appkey": @"tospur123",
                           @"signature": signature,
                           @"sessionid": self.sessionid,
                           @"version": @"1.0",
                           };
    [tx POST:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);
        NSArray *array = [responseObject objectForKey:@"ImaList"];
        
        for (NSMutableDictionary *dic in array) {
            NSString *str = [dic objectForKey:@"ImgUrl"];
            
            [imageUrlArray addObject:str];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
    return imageUrlArray;
}
- (void)requestBuildDetailWithGardenId:(NSString *)gardenId success:(void (^)(BuildDetailModel *buildDetailModel))successed failed:(void (^)(NSError *error))failed{
    if(!gardenId){
        return;
    }
    else {
        AFHTTPRequestOperationManager *tx = [AFHTTPRequestOperationManager manager];
        //申明返回的结果是json类型
        tx.responseSerializer = [AFJSONResponseSerializer serializer];
        //申明请求的数据是json类型
        tx.requestSerializer = [AFJSONRequestSerializer serializer];
        // [tx.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
        NSString *urlStr = @"http://172.20.5.142:7001/api/GetGardenDetail";
        NSString *signature = [[Encryption shareInstance]sha1];
        NSLog(@"+++++ %@",signature);
        NSDictionary *dict = @{
                               @"appkey": @"tospur123",
                               @"signature": signature,
                               @"sessionid": self.sessionid,
                               @"GardenId" : gardenId
                               };
        [tx POST:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"%@",responseObject);
            BuildDetailModel *buildDetailModel = [[BuildDetailModel alloc]init];
            [buildDetailModel setValuesForKeysWithDictionary:responseObject];
            successed(buildDetailModel);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
            failed(error);
            
        }];
        
        
    }
    
}

@end
